package com.isias.system.Controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isias.common.result.Result;
import com.isias.model.system.SysRole;
import com.isias.model.vo.AssginRoleVo;
import com.isias.model.vo.SysRoleQueryVo;
import com.isias.system.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {
    @Autowired
    SysRoleService service;


    /**
     * 根据用户获取角色数据
     */

    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@PathVariable String userId) {
        Map<String, Object> roleMap = service.getRolesByUserId(userId);
        return Result.ok(roleMap);
    }

    /**
     * 根据用户分配角色
     */
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo) {
        if (service.doAssign(assginRoleVo)) {
            return Result.ok();
        }else {
            return Result.fail();
        }
    }


    /**
     * 通过id逻辑删除
     */
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @DeleteMapping("{id}")
    public Result DeleteByID(@PathVariable String id){
        if (service.removeById(id)) {
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    /**
     * 查询全部
     */
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @GetMapping()
    public Result SelectAll(){
        List<SysRole> list = service.list();
//        if (true) throw new RuntimeException();
        return Result.ok(list);
    }
    /**
     * 分页查询
     */

    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @GetMapping("/{page}/{limit}")
    public Result Page(@PathVariable Integer page,@PathVariable Integer limit,SysRoleQueryVo roleQueryVo){
        Page<SysRole> pageparam = new Page<>();
        pageparam.setCurrent(page);
        pageparam.setSize(limit);
        IPage<SysRole> iPage = service.selectPage(pageparam,roleQueryVo);
        return Result.ok(iPage);
    }
    /**
     * 通过id查询单个
     */
    @GetMapping("{id}")
    public Result SelectByID(@PathVariable Integer id){
        SysRole sysRole = service.getById(id);
        return Result.ok(sysRole);
    }
    /**
     * 添加
     */
    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @PostMapping()
    public Result Add(@RequestBody SysRole sysRole){
        if (service.save(sysRole)) {
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    /**
     * 修改
     */
    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @PutMapping()
    public Result Update(@RequestBody SysRole sysRole){
        if (service.updateById(sysRole)) {
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    /**
     * 根据id列表删除
     */
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        if (service.removeByIds(idList)) {
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

}
